﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using SDSystem.Models;
using System.Linq.Dynamic;

namespace SDSystem.Controllers
{
    public class SICController : Controller
    {
        #region demo data
        private List<TypeOfBusiness> SampleData = new List<TypeOfBusiness>
                                                      {
                                                          new TypeOfBusiness{BusinessId = 1, SICcode = 1001,BusinessName = "Business type 1"},
                                                          new TypeOfBusiness{BusinessId = 2, SICcode = 1002,BusinessName = "Business type 2"},
                                                          new TypeOfBusiness{BusinessId = 3, SICcode = 1003,BusinessName = "Business type 3"},
                                                          new TypeOfBusiness{BusinessId = 4, SICcode = 1004,BusinessName = "Business type 4"},
                                                          new TypeOfBusiness{BusinessId = 5, SICcode = 1005,BusinessName = "Business type 5"},
                                                          new TypeOfBusiness{BusinessId = 6, SICcode = 1006,BusinessName = "Business type 6"},
                                                          new TypeOfBusiness{BusinessId = 7, SICcode = 1007,BusinessName = "Business type 7"},
                                                          new TypeOfBusiness{BusinessId = 8, SICcode = 1008,BusinessName = "Business type 8"},
                                                      };

        #endregion

        private SDSystemEntities db = new SDSystemEntities();

        //
        // GET: /SIC/Lookup

        [Authorize(Roles = "users")]
        public ActionResult Lookup()
        {
            return View();
        }

        // Search
        [Authorize(Roles = "users")]
        public ActionResult Search()
        {
            var store = X.GetCmp<Store>("DataStore");
            // search here

            Session["sSICcode"] = X.GetCmp<TextField>("txtSICcode").Text;
            Session["sName"] = X.GetCmp<TextField>("txtName").Text;

            store.Reload();
            return this.Direct();
        }

        [Authorize(Roles = "users")]
        public ActionResult ClearSearch()
        {
            var store = X.GetCmp<Store>("DataStore");
            // Clear search here
            Session["sSICcode"] = X.GetCmp<TextField>("txtSICcode").Text = "";
            Session["sName"] = X.GetCmp<TextField>("txtName").Text = "";

            store.Reload();
            return this.Direct();
        }

        [Authorize(Roles = "users")]
        public ActionResult BindingStore(StoreRequestParameters parameters)
        {
            // Search
            var siccode = 0;
            if (!string.IsNullOrEmpty((string)Session["sSICcode"]))
            {
                Int32.TryParse((string) Session["sSICcode"], out siccode);
            }
            var name = (string)Session["sName"];

            var result = db.TypeOfBusinesses.Where(ent =>
                   (siccode == 0 || ent.SICcode == siccode) &&
                   (string.IsNullOrEmpty(name) || ent.BusinessName.Contains(name))
                );

            // Sorting
            if (!string.IsNullOrEmpty(parameters.SimpleSort))
            {
                result = result.OrderBy(parameters.SimpleSort + (parameters.SimpleSortDirection == SortDirection.DESC ? " desc" : " asc"));
            }
            else
            {
                result = result.OrderByDescending(ent => ent.BusinessId);
            }

            // Paging
            int limit = parameters.Limit;
            if ((parameters.Start + parameters.Limit) > result.Count())
            {
                limit = result.Count() - parameters.Start;
            }
            var rangeData = (parameters.Start < 0 || limit < 0) ? result : result.Skip(parameters.Start).Take(limit);
            
            return this.Store(rangeData, result.Count());
        }

    }
}
